package br.com.cnpv.modelo.bo;

import java.util.List;

import org.hibernate.Session;

import br.com.cnpv.modelo.acesso.idao.ICombustivelDAO;
import br.com.cnpv.modelo.dao.CombustivelDAO;
import br.com.cnpv.modelo.entity.Combustivel;
import br.com.cnpv.util.HibernateUtil;

public class CombustivelBO extends HibernateUtil{
	
	public static CombustivelBO INSTANCE;
	
	public static CombustivelBO getInstance()
	{
		if(INSTANCE == null)
		{
			synchronized (CombustivelBO.class) {
				INSTANCE = new CombustivelBO();
			}
		}
		return INSTANCE;
	}

	public List<Combustivel> buscar(Combustivel combustivel) throws Exception{
		Session session = null;
		List<Combustivel> list = null;
		try
		{
			session = currentSession();
			ICombustivelDAO dao = new CombustivelDAO();
			list = dao.buscar(combustivel, session);
		}catch(Exception e)
		{
			throw new Exception(e);
		}finally
		{
			if(session != null)
			{
				session.flush();
				session.clear();
				closeSession();
			}
		}
		return list;
	}

	public void saveOrUpdate(Combustivel combustivel) throws Exception{
		Session session = null;
		try
		{
			session = currentSession();
			ICombustivelDAO dao = new CombustivelDAO();
			dao.saveOrUpdate(combustivel, session);
		}catch(Exception e)
		{
			throw new Exception(e);
		}finally
		{
			if(session != null)
			{
				session.flush();
				session.clear();
				closeSession();
			}
		}
	}

	public List<Combustivel> listar() throws Exception
	{
		Session session = null;
		List<Combustivel> list = null;
		try
		{
			session = currentSession();
			ICombustivelDAO dao = new CombustivelDAO();
			list = dao.listar(session);
		}catch(Exception e)
		{
			throw new Exception(e);
		}finally
		{
			if(session != null)
			{
				session.clear();
				session.flush();
			}
			closeSession();
		}
		return list;
	}
}